{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "dominant-chile",
   "metadata": {},
   "source": [
    "# Example 14.3 Adiabatic Compression and Expansion"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "responsible-party",
   "metadata": {},
   "source": [
    "A heat pump using the refrigerant R-22 operates with a mass flow rate of 100 kg/hr. The fluid enters the compressor at T1 = 300 K and P1 = 1 bar. The compressor heat loss is neglected. The outlet pressure of the compressor is 5 bar. If the isentropic efficiency of the compressor is 0.7 and the mechanical efficiency is 0.9, what is the power draw of the compressor and how how is the refrigerant when it exits the compressor?\n",
    "\n",
    "The textbook uses the Peng-Robinson EOS, so to compare, use that as well."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "marine-covering",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set the conditions and imports\n",
    "from scipy.constants import bar, hour\n",
    "from thermo import ChemicalConstantsPackage, PRMIX, CEOSLiquid, CEOSGas, FlashPureVLS\n",
    "fluid = 'R-22'\n",
    "constants, correlations = ChemicalConstantsPackage.from_IDs([fluid])\n",
    "\n",
    "T1 = 300.0\n",
    "P1 = 1*bar\n",
    "P2 = 5*bar\n",
    "eta_isentropic = 0.7\n",
    "eta_mechanical = 0.9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "alike-commercial",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The actual power is 2251 W\n",
      "The actual outlet temperature is  405.98 K\n"
     ]
    }
   ],
   "source": [
    "# Use the default originally published Peng-Robinson models\n",
    "eos_kwargs = dict(Tcs=constants.Tcs, Pcs=constants.Pcs, omegas=constants.omegas)\n",
    "liquid = CEOSLiquid(PRMIX, HeatCapacityGases=correlations.HeatCapacityGases, eos_kwargs=eos_kwargs)\n",
    "gas = CEOSGas(PRMIX, HeatCapacityGases=correlations.HeatCapacityGases, eos_kwargs=eos_kwargs)\n",
    "flasher = FlashPureVLS(constants, correlations, gas=gas, liquids=[liquid], solids=[])\n",
    "\n",
    "# Flash at inlet conditions to obtain initial enthalpy\n",
    "state_1 = flasher.flash(T=T1, P=P1)\n",
    "# Flash at outlet condition - entropy is conserved by compressors and expanders!\n",
    "state_2_ideal = flasher.flash(S=state_1.S(), P=P2)\n",
    "# Compute the change in enthalpy\n",
    "delta_H_ideal = (state_2_ideal.H()-state_1.H())\n",
    "# The definition of isentropic efficiency means that the actual amount of heat added is\n",
    "# dH_actual = dH_idea/eta_isentropic\n",
    "H_added_to_fluid_actual = delta_H_ideal/eta_isentropic\n",
    "\n",
    "state_2 = flasher.flash(H=state_1.H() + H_added_to_fluid_actual, P=P2)\n",
    "\n",
    "# To compute the actual power, itis more convinient to use the mass enthalpy\n",
    "actual_power_per_kg = (state_2.H_mass() - state_1.H_mass())/(eta_mechanical) # W/kg\n",
    "actual_power = actual_power_per_kg*100/hour\n",
    "print(f'The actual power is {actual_power:.0f} W')\n",
    "print(f'The actual outlet temperature is {state_2.T: .2f} K')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "associate-motion",
   "metadata": {},
   "source": [
    "The power given in the textbook is 2257 W and 405.68 K out. No details as to the liquid heat capacity are given. As refrigerants are well defined substances, it is recommended for anyone doing modeling with them to use a high-accuracy model wherever possible."
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
